<template>
	<view class="classlist">
		<view class="loadingLayout" v-if="!classList.length && !noData">
			<uni-load-more status="loading"></uni-load-more>
		</view>
		<view class="content">
			<navigator class="item" :url="`/pages/preview/preview?id=${item._id}`" v-for="item in classList"
				:key="item._id">
				<image :src="item.smallPicurl" mode="aspectFill"></image>
			</navigator>
		</view>
		<view class="loadingLayout" v-if="classList.length || noData">
			<uni-load-more :status="noData?'noMore' : 'loading'"></uni-load-more>
		</view>
		<view class="safe-area-inset-bottom">
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		onLoad,
		onUnload,
		onReachBottom,
		onShareAppMessage,
		onShareTimeline
	} from "@dcloudio/uni-app"
	import {
		getClassListAPI,
		getUserWallListAPI
	} from '@/apis/index.js'
	const classList = ref([])
	const noData = ref(false)
	const getClassList = async () => {
		let res;
		if (queryParams.classid) res = await getClassListAPI(queryParams)
		if (queryParams.type) res = await getUserWallListAPI(queryParams)
		if (queryParams.pageSize > res.data.data.length) noData.value = true
		classList.value = [...classList.value, ...res.data.data]
		// 数据写入缓存
		uni.setStorageSync("storgClassList", classList.value)
	}
	const queryParams = {
		pageNum: 1,
		pageSize: 12
	}
	let pageName
	onLoad((e) => {
		let {
			id,
			name,
			type
		} = e
		if (type) queryParams.type = type
		if (id) queryParams.classid = id
		pageName = name
		uni.setNavigationBarTitle({
			title: name
		})
		getClassList()
	})

	onUnload(() => {
		uni.removeStorageSync('storgClassList')
	})
	onReachBottom(() => {
		// if (noData.value) return uni.showModal({
		// 	content: '数据加载完毕'
		// })
		queryParams.pageNum++
		getClassList()
	})
	onMounted(() => {})
	// 分享微信好友
	onShareAppMessage((e) => {
		return {
			title: pageName,
			path: `/pages/classlist/classlist?id=${queryParams.classid}&name=${pageName}`
		}
	})
	// 分享朋友圈
	onShareTimeline(() => {
		return {
			title: 'uni壁纸项目',
			query: `id=${queryParams.classid}&name=${pageName}`
		}
	})
</script>

<style lang="scss" scoped>
	.classlist {
		.content {
			padding: 5rpx;
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 5rpx;

			.item {
				height: 440rpx;

				image {
					width: 100%;
					height: 100%;
					display: block;
				}
			}
		}
	}
</style>